package tsinghua.smxq.mc;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import tsinghua.smxq.utilitiespayment.bankAccount;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ICBCLogonActivity extends Activity {
	String cardNum; // 卡号
	String password; // 密码
	int type; // 缴费类型
	// bankAccount bankaccount; // 银行账户
	int status; // 登录情况
	String recordId;
	String cityId;
	int month;
	float usage;
	Button back;

	float unitprice;
	float rate;
	String result;
	EditText cardnum_edittext, password_edittext;
	Button logon_button;
	private Toast toast;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_icbc_logon);
		type = getIntent().getIntExtra("type", -1);
		recordId = getIntent().getStringExtra("recordId");
		cityId = getIntent().getStringExtra("cityId");
		month = getIntent().getIntExtra("month", -1);
		usage = getIntent().getFloatExtra("usage", 0.0f);
		unitprice = getIntent().getFloatExtra("unitprice", 0.0f);
		rate = getIntent().getFloatExtra("rate", 0.0f);
		back = (Button) findViewById(R.id.icbclogon_back);
		back.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ICBCLogonActivity.this.finish();
			}

		});

		cardnum_edittext = (EditText) findViewById(R.id.cardnum_edittext_logon);
		password_edittext = (EditText) findViewById(R.id.psw_edittext_logon);
		logon_button = (Button) findViewById(R.id.button_logon);
		logon_button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				cardNum = cardnum_edittext.getText().toString();
				password = password_edittext.getText().toString();
				if (cardNum.length() == 19) {
					// 连接服务器端，判断账号密码是否正确
					HttpClient httpclient = new DefaultHttpClient();
					ArrayList<NameValuePair> qparams = new ArrayList<NameValuePair>();
					qparams.add(new BasicNameValuePair("method",
							"getBankAccount"));
					qparams.add(new BasicNameValuePair("cardno", String
							.valueOf(cardNum)));
					qparams.add(new BasicNameValuePair("password", String
							.valueOf(password)));
					URI uri;
					try {
						uri = URIUtils.createURI("http", Constants.serverIP,
								8080,
								"MobileCityServer/ICBCINBSEBusinessServlet",
								URLEncodedUtils.format(qparams, "UTF-8"), null);
						HttpGet httpget = new HttpGet(uri);
						HttpResponse response = httpclient.execute(httpget);
						HttpEntity entity = response.getEntity();
						if (entity != null) {
							String contentString = EntityUtils.toString(entity);
							JSONObject o = new JSONObject(contentString);
							status = o.getInt("status");
							if (status == 1) {
								// 跳转到bank界面
								Intent gotoIntent = new Intent(
										ICBCLogonActivity.this,
										BankActivity.class);
								gotoIntent.putExtra("recordId", recordId);
								gotoIntent.putExtra("cardNum",
										o.getString("cardno"));
								gotoIntent.putExtra("password",
										o.getString("password"));
								gotoIntent.putExtra("money",
										o.getString("depo"));
								gotoIntent.putExtra("type", type);
								gotoIntent.putExtra("month", month);
								gotoIntent.putExtra("usage", usage);
								gotoIntent.putExtra("unitprice", unitprice);
								gotoIntent.putExtra("rate", rate);
								gotoIntent.putExtra("cityId", cityId);
								ICBCLogonActivity.this
										.startActivity(gotoIntent);
							} else {
								DisplayToast("账号或密码错误，登录失败", Toast.LENGTH_SHORT);
								password_edittext.setText("");
							}
						}

					} catch (URISyntaxException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				} else {
					DisplayToast("卡号正确长度为19位！", Toast.LENGTH_SHORT);
				}
			}
		});

	}

	// 显示信息
	public void DisplayToast(String str, int time) {
		toast = Toast.makeText(this, str, time);
		// 设置toast显示的位置
		toast.setGravity(Gravity.TOP, 0, 320);
		// 显示该Toast
		toast.show();
	}

}
